package cn.geminis.workflow.service.utils;

import cn.geminis.data.jpa.PageQuery;
import org.flowable.common.engine.api.query.Query;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;

import java.util.List;

/**
 * @author puddi
 */
public class QueryUtils {

    public static <T> List<T> toList(Query query, Class<T> clazz) {
        var queryResult = query.list();
        return ObjectUtils.convert(queryResult, clazz);
    }

    public static <T> Page<T> toPage(Query query, PageQuery pageParameter, Class<T> clazz) {
        var total = query.count();
        var queryResult = query.listPage(pageParameter.getPageIndex(), pageParameter.getPageSize());
        var returnResult = ObjectUtils.convert(queryResult, clazz);

        return new PageImpl<T>(returnResult, pageParameter.toPageable(), total);
    }

}
